//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    item: {
      mode: "aspectFill"
    },
    image: {
      header: "",
      scan: "",
      input: "",
    },
    // visible:"hidden",
    visible: "visible",
    hiddenLoading: true,
  },
  bindInput:function(){
    wx.navigateTo({
      url: 'inputInfo',
    })
  },
  //事件处理函数
  takephoto: function() {
    // wx.navigateTo({
    //   url:'code'
    // })

    var that = this
    this.setData({
      code_data: "",
      visible: "hidden",
    })
    wx.scanCode({
      success: function(res) {
        console.log(res)
        that.setData({
          hiddenLoading: false,
        })
        var yzmurl = 'https://wx.fapiaohelp.com/verify-service/invoice/check?'
        var ar = res["result"].split(',')
        var fpdm = ar[2]
        console.log('fpdm:' + fpdm)
        var fphm = ar[3]
        console.log('fphm:' + fphm)
        var kprq = ar[5]
        console.log('kprq:' + kprq)
        var kjje = ar[4]
        console.log('kjje:' + kjje)
        var jym = ar[6]
        console.log('jym:' + jym)
        var invoiceType = util.getInvoiceType(fpdm)
        var ext
        if (invoiceType == '01') {
          ext = kjje
        } else {
          ext = jym.substr(jym.length - 6, 6)
        }
        console.log('ext:' + ext)
        yzmurl = yzmurl + 'fpdm=' + fpdm + '&fphm=' + fphm + '&kprq=' + kprq + '&ext=' + ext
        console.log(yzmurl)
        wx.request({
          url: yzmurl,
          success: function(res) {
            console.log(res.data)

            if (res.data["returnCode"] < 0) {
              wx.showModal({
                title: '错误',
                content: res.data["errorMessage"],
                showCancel: false,
                complete: function() {
                  console.log("错误：" + res.data["errorMessage"])
                }
              })
            } else {
              wx.setStorage({
                key: 'detail',
                data: res.data,
                success: function(res) {
                  wx.navigateTo({
                    url: 'code?detail=' + res.data,
                  })
                }
              })
            }
          },
          complete: function(res) {
            that.setData({
              hiddenLoading: true,
            })
          }

        })
      }
    })
  },
  getdetail: function() {
    var that = this
    wx.navigateTo({
      url: 'code?code=' + this.data.code + '&id=' + this.data.id
    })
  },
  change: function(e) {
    var that = this
    this.setData({
      code: e.detail.value
    })
  },
  onLoad: function() {
    var that = this
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    // wx.request({
    //   url: 'https://www.tax123.xin/apiv1/face/index',
    //   success: function (res){
    //     that.setData({
    //       "image.header": res.data["data"]["headerImgData"],
    //       "image.scan": res.data["data"]["scanImgData"],
    //       "image.input": res.data["data"]["inputImgData"],
    //     })
    //   }
    // })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})